<template>
  <div>
    <div class="block">
      <p>使用 scoped slot</p>
      <el-tree :data="data1"
               node-key="id"
               default-expand-all
               :expand-on-click-node="false">
        <span class="custom-tree-node"
              slot-scope="{ node, data }">
          <span>{{ data.label}}</span>
          <span>
            <el-button type="text"
                       size="mini"
                       class='el-icon-plus'
                       @click="() => append(data)">
            </el-button>
            <el-button type="text"
                       size="mini"
                       class='el-icon-delete'
                       @click="() => remove(node, data)">
            </el-button>
            <el-button type="text"
                       size="mini"
                       :class="{'white': data.subscript==0 }"
                       class='el-icon-top'
                       @click="() => upNode(node, data)">
              <!-- z -->
            </el-button>
            <el-button type="text"
                       size="mini"
                       :class="{'dis': data.subscript==data1.length-1}"
                       class='el-icon-bottom'
                       @click="() => downNode(node, data)">
            </el-button>

          </span>
          <!-- :class="{'white': node.parent.parent || node.parent.data.children  ? node.parent.data.children[0].id == data.id : false }" -->
        </span>
      </el-tree>
    </div>

  </div>
</template>
<script>
let id = 1000;
export default {
  data () {

    return {
      data1: [],
    }
  },
  mounted () { // 页面进入时加载内容
    const data = [{
      id: 1,
      label: '一级 1',
      children: [{
        id: 4,
        label: '二级 1-1',
        children: [{
          id: 9,
          label: '三级 1-1-1'
        }, {
          id: 10,
          label: '三级 1-1-2'
        }]
      }]
    }, {
      id: 2,
      label: '一级 2',
      children: [{
        id: 5,
        label: '二级 2-1'
      }, {
        id: 6,
        label: '二级 2-2'
      }]
    }, {
      id: 3,
      label: '一级 3',
      children: [{
        id: 7,
        label: '二级 3-1'
      }, {
        id: 8,
        label: '二级 3-2'
      }]
    }];
    data.map((item, index) => {
      item.subscript = index
    })

    this.data1 = data
    console.log(this.data1)
  },
  methods: {
    // :class="{'white':node.parent.data.children[0] ? node.parent.data.children[0].id !== data.id : false }"
    // 上移
    // 因为上移和下移操作中children中数据的变化不能直接响应到data中所以需要手动去响应
    upNode (node, data) {
      const parent = node.parent;
      console.log('父亲的node', node);
      console.log('data的数据', data);
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id)
      if (index > 0) {
        children[index] = children.splice(index - 1, 1, children[index])[0]
      }
      // const obj = JSON.stringify(parent.data.children)
      // this.data1 = JSON.parse(JSON.stringify(this.data1).replace(/obj/g,children))
    },
    // 下移
    downNode (node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id)
      if (index < children.length - 1) {
        children[index] = children.splice(index + 1, 1, children[index])[0]
      }
      // const obj = JSON.stringify(parent.data.children)
      // this.data1 = JSON.parse(JSON.stringify(this.data1).replace(/obj/g, children))
    },
    // 追加节点
    append (data) {
      this.$prompt('请输入目录名称', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
        inputErrorMessage: '不能为空'
      }).then(({ value }) => {
        console.log('打印一下data中的数据', data);
        console.log('打印一下添加的数据', value);
        const newChild = { id: id++, label: value, children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      }).catch(() => {

      });
    },
    remove (node, data) {
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex(d => d.id === data.id);
      children.splice(index, 1);
      console.log('打印一下children的数据', children);
      console.log('打印一下data的数据', this.data);
    }
  }
};
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.white {
  color: #f2f3f3 !important;
  cursor: not-allowed !important;
}
.dis {
  color: #f2f3f3 !important;
  cursor: not-allowed !important;
}
</style>